package com.maila.calltaxi.view.timepicker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;

import com.maila.calltaxi.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.maila.calltaxi.view.timepicker.TimeUtils.getStatetime;


/**
 * Created by Okamiy on 2017/6/22.
 * Email: 542839122@qq.com
 */

public class TimeSelectUtils implements NumberPicker.OnValueChangeListener, View.OnClickListener {


    private String initDateTime;
    private Context activity;
    private Calendar calendar;
    private CustomNumberPicker hourpicker;
    private CustomNumberPicker minutepicker;
    private CustomNumberPicker datepicker;
    private String[] minuteArrs;
    private String[] hourArrs;
    private String hourStr;
    private String minuteStr;
    private String dateStr;
    private Dialog dialog;
    private String[] dayArrays;
    private long currentTimeMillis;

    private String startTime = "";
    private GetSubmitTime mSubTime;
    int today=1;
    public TimeSelectUtils(Context activity, String initDateTime, GetSubmitTime subTime) {
        this.activity = activity;
        this.initDateTime = initDateTime;
        this.mSubTime = subTime;
    }

    public void initPicker() {
        Calendar calendar = Calendar.getInstance();

        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);

        if (45 <= minutes)
            minutes = 0;

        else if (30 <= minutes)
            minutes = 3;

        else if (15 <= minutes)
            minutes = 2;

        else
            minutes = 1;

        // 设置日期 2天以内
        dayArrays = new String[3];
        dayArrays[0] = "今天";
        dayArrays[1] = "明天";
        dayArrays[2] = "后天";
        switch (dayArrays.length) {
            case 0:
                getStatetime();
                break;
            case 1:
                getStatetime(1);
                break;
        }
        currentTimeMillis = System.currentTimeMillis();// 设置当前时间的毫秒值
        datepicker.setOnValueChangedListener(this);
        datepicker.setDisplayedValues(dayArrays);
        datepicker.setMinValue(0);
        datepicker.setMaxValue(dayArrays.length - 1);
        datepicker.setValue(0);
        dateStr = dayArrays[0];// 初始值

        // 设置分钟
        setHours();

        // 设置分钟
        minuteArrs = new String[]{"00分", "20分", "30分", "40分","50分"};
        minutepicker.setOnValueChangedListener(this);
        minutepicker.setDisplayedValues(minuteArrs);
        minutepicker.setMinValue(0);
        minutepicker.setMaxValue(minuteArrs.length - 1);
        minutepicker.setValue(minutes);
        minuteStr = minuteArrs[minutes];// 初始值
    }

    private void setHours() {
        if(today==1){
            Calendar rightNow = Calendar.getInstance();
            int hour = rightNow.get(Calendar.HOUR_OF_DAY);
            hourArrs=new String[25-hour];
            hourArrs[0]="现在";

            for(int i=0;i<24-hour;i++){
                hourArrs[i+1]=hour+i+"点";
            }
        }else {
            hourArrs=new String[24];
            for(int i=0;i<24;i++){
                hourArrs[i]=i+"点";
            }
        }


        hourpicker.setOnValueChangedListener(this);
        hourpicker.setDisplayedValues(hourArrs);
        hourpicker.setMinValue(0);
        hourpicker.setMaxValue(hourArrs.length - 1);
        hourpicker.setValue(1);
        hourStr = hourArrs[1];// 初始值
    }

    /**
     * 弹出日期时间选择框方法
     *
     * @return
     */
    @SuppressWarnings("deprecation")
    public Dialog dateTimePicKDialog() {
        View dateTimeLayout = View.inflate(activity, R.layout.item_time_select,
                null);
        dateTimeLayout.findViewById(R.id.tv_cancel).setOnClickListener(this);
        dateTimeLayout.findViewById(R.id.tv_confirm).setOnClickListener(this);


        datepicker = (CustomNumberPicker) dateTimeLayout
                .findViewById(R.id.datepicker);
        hourpicker = (CustomNumberPicker) dateTimeLayout
                .findViewById(R.id.hourpicker);
        minutepicker = (CustomNumberPicker) dateTimeLayout
                .findViewById(R.id.minuteicker);
        datepicker
                .setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        hourpicker
                .setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        minutepicker
                .setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        datepicker.setNumberPickerDividerColor(datepicker);
        hourpicker.setNumberPickerDividerColor(hourpicker);
        minutepicker.setNumberPickerDividerColor(minutepicker);
        initPicker();
        dialog = new Dialog(activity, R.style.dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(dateTimeLayout, new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams
                .WRAP_CONTENT));

        Window window = dialog.getWindow();
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = ((Activity) activity).getWindowManager().getDefaultDisplay()
                .getHeight();
        wl.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        wl.height = RelativeLayout.LayoutParams.WRAP_CONTENT;

        dialog.onWindowAttributesChanged(wl);
        // 设置点击外围隐藏
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();

        onDateChanged();
        return dialog;
    }

    @SuppressWarnings("deprecation")
    public void onDateChanged() {
        // 获得日历实例
        calendar = Calendar.getInstance();
        calendar.setTime(new Date(currentTimeMillis));
        Date date = calendar.getTime();
        if(hourStr.equals("现在")){
            calendar.setTime(date);
            initDateTime="现在";
        }else {
            date.setHours(Integer.parseInt(hourStr.replace("点","")));
            date.setMinutes(Integer.parseInt(minuteStr.replace("分","")));
            calendar.setTime(date);
            if (calendar.getTimeInMillis() <= System.currentTimeMillis() + 900000
                    || calendar.getTimeInMillis() > System.currentTimeMillis() + 2
                    * 24 * 3600 * 1000) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                initDateTime = sdf.format(calendar.getTime()) + " " + hourStr.replace("点","") + ":"
                        + minuteStr.replace("分","");
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                initDateTime = sdf.format(calendar.getTime()) + " " + hourStr.replace("点","") + ":"
                        + minuteStr.replace("分","");
            }
        }

    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.datepicker:
                currentTimeMillis = System.currentTimeMillis() + newVal * 24 * 3600 * 1000;
                dateStr = dayArrays[newVal];
                if(dateStr.equals("今天")){
                    today=1;
                }else {
                    today=0;
                    minutepicker.setVisibility(View.VISIBLE);
                }
                setHours();
                onDateChanged();
                break;
            case R.id.hourpicker:
                hourStr = hourArrs[newVal]+ "";
                if(hourStr.equals("现在")){
                    minutepicker.setVisibility(View.INVISIBLE);
                }else {
                    minutepicker.setVisibility(View.VISIBLE);
                }
                onDateChanged();
                break;
            case R.id.minuteicker:
                minuteStr = minuteArrs[newVal];
                onDateChanged();
                break;
            default:
                break;
        }
    }

    public interface GetSubmitTime {
        void selectTime(String data);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                dialog.dismiss();
                break;
            case R.id.tv_confirm:
                selectTimes();
                dialog.dismiss();
                break;

            default:
                break;
        }
    }

    private void selectTimes() {
        mSubTime.selectTime(initDateTime);

    }


}
